home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c
- Subject: Re: Trouble Reading Binary Data from MicroVAX III
- Date: 16 Apr 1996 09:02:55 GMT
- Organization: systems hk
- Message-ID: <4kvnnv$o4o@nadine.teleport.com>
- References: <4kh8d8$kjc@newshound.csrv.uidaho.edu> <4ktn69INNohu@keats.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: ip-pdx02-45.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) wrote:
- >In article <4kh8d8$kjc@newshound.csrv.uidaho.edu>,
- >Ron Patterson <patt9451@uidaho.edu> wrote:
- > >Hello all,
- > >
- > >I am having trouble reading binary data that was created with a MicroVAX III
- > >workstation. I am able to read int's, and char's o.k. but the majority of
- > >the data is a large number of floating point values. fread() returns the
- > >correct number of floating point numbers read but the data is a mess of zeros
- > >and extremely large or small numbers. Is this a big endian/little endian
- > >problem (my program is being compiled and run on a PC with a 32 bit C compiler)
- > >or bit ordering problem? I know the size of a float on both systems is the
- > >same (4 bytes). Any help would be great!
- >
- >The Vax doesn't use IEEE floating point representation, as far as I remember. I
- >don't have references off-hand that would tell me what that representation is,
- >but all I can say is: be prepared to do some bit twiddling to recover the
- >values.
- >
- >One of the textbooks by William Stallings, or just about any general text on
- >computer architectures, should have a description.
- >--
- >I'm not really a jerk, but I play one on Usenet.
-
- Ron,
-
- Here is a routine I found somewhere. I take no credit or blame for its look
- or feel. It does seem to work for my MicroStation CAD work (which exists on
- VAXen and Intels). Yours to keep.
-
- Good Luck,
- Geoff Houck
- hksys@teleport.com
-
- /* ---------------------------------------------------------------------------
- vaxToIntel - Converts a vax float_d stored as 8 bytes to an Intel double.
- --------------------------------------------------------------------------- */
- double vaxToIntel ( double *vaxFloat )
- {
- unsigned char vaxBytes[8];
- long lngBytes[8];
- int i, j, k;
- int res, temp, bits[64], sub, sign;
- double exp, frac, f1, result;
-
- memmove( vaxBytes,vaxFloat,8 );
- for( i=0; i<8; i++ )
- lngBytes[i] = vaxBytes[i];
-
- for( j=0; j<8; j++ ) {
- switch( j ) {
- case 0:
- k = 1;
- break;
- case 1:
- k = 0;
- break;
- case 2:
- k = 3;
- break;
- case 3:
- k = 2;
- break;
- case 4:
- k = 5;
- break;
- case 5:
- k = 4;
- break;
- case 6:
- k = 7;
- break;
- case 7:
- k = 6;
- break;
- }
-
- temp = lngBytes[k];
- sub = 128;
-
- for( i=(j*8); i<(j*8)+7; ++i ) {
- res = temp / sub;
- bits[i] = res;
- if( res == 1 ) {
- temp = temp - sub;
- sub = sub / 2;
- }
- bits[(j*8)+7] = temp;
- }
- }
-
- sign = bits[0];
- exp = 0;
- res = 0;
- sub = 128;
-
- for( i=1; i<9; i++ ) {
- res = res + ( bits[i] * sub );
- sub = sub/2;
- }
-
- res = res - 128;
- exp = pow( 2,res );
- frac = .5;
- f1 = .25;
-
- for( i=9; i<65; i++ ) {
- frac = frac + ( bits[i] * f1 );
- f1 = f1/2;
- }
-
- result = frac * exp;
- if( sign == 1 ) {
- result = 0 - result;
- }
- return ( result );
- }
-
-
-